package com.exam.util;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import net.sf.json.JSONArray;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;

public class Tools {
	
	 public static JSONArray getJsonToArray(Object obj) {
		 JSONArray jsonArray = JSONArray.fromObject(obj);
		 return jsonArray;       
	 }
	
	public static String encodeBase64(String str){
		return new String(Base64.encodeBase64(str.getBytes(), true));
	}
	
	public static String decodeBase64(String str){
		return new String(Base64.decodeBase64(str.getBytes()));
	}
	
	
	public static long getTestTime(String date,int testTime) throws ParseException{
		Date d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date);
		long m = testTime*60*1000-(new Date().getTime()-d.getTime());
		if(m>=0){
			return m/1000;
		}else{
			return 0;
		}
	}
	
	
	
	public static String getIpAddress() throws UnknownHostException {   
        InetAddress address = InetAddress.getLocalHost();   
        return address.getHostAddress();   
    }  
	
	public static String sha1(String str){
		return DigestUtils.sha1Hex(str);
	}
	
	public static String getNowDate(){
		return new SimpleDateFormat("�����ǣ�yyyy��MM��dd�� E").format(new Date());
	}
	
	public static String dateDir(){
		return new SimpleDateFormat("yyyyMMdd").format(new Date());
	}
	
	public static String formatDate(String date){
		try {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm").format((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date)));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
		public static boolean isNumeric(String str){  
			  for (int i = str.length();--i>=0;){    
			   if (!Character.isDigit(str.charAt(i))){  
			    return false;  
			   }  
			  }  
			  return true;  
			}
	
	/**
	 * 
	 * Description:转义html
	 * @author: 刘楠
	 * @date: 2017年4月19日
	 * @param content
	 * @return String
	 */
	 public static String escapeHTML(String content) {
		 if(content==null) return "";        
		     String html = content;
		     html = html.replace( "'", "&apos;");
		     html = html.replaceAll( "&", "&amp;");
		     html = html.replace( "\"", "&quot;");  //"
		     html = html.replace( "\t", "&nbsp;&nbsp;");// �滻���
		     html = html.replace( " ", "&nbsp;");// �滻�ո�
		     html = html.replace("<", "&lt;");
		     html = html.replaceAll( ">", "&gt;");
		     return html;
		 }
	 
	 public static String getHTML(String content) {
		 if(content==null) return "";        
		     String html = content;
		     html = html.replaceAll("&lt;","<");
		     html = html.replaceAll(  "&gt;",">");
		     html = html.replaceAll( "&apos;","'");
		     html = html.replaceAll( "&amp;","&");
		     html = html.replaceAll( "&quot;","\"");  
		     html = html.replaceAll(  "&nbsp;&nbsp;" ,"\t");
		     html = html.replaceAll(  "&nbsp;"," ");
		     return html;
		 }
	 
	 
	 public static String getUTF8Str(String str){
		 String utf8 = "";
		 try {
			utf8 =  new String(str.getBytes("ISO8859-1"),"utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		 return utf8;
	 }
	 
	 /**
	  * 
	  * Description:
	  * @author: 刘楠
	  * @date: 2017年4月19日
	  * @param data
	  * @param max
	  * @return
	  */
	 public static String subStr(String data,int max){
		 if(data.length()>=max){
			 data = data.substring(0, max)+"..";
		 }
		 return data;
	 } 
	
}
